<?php
session_start();

// 检查用户是否已登录
if (!isset($_SESSION['user_id'])) {
    header('Location: login21.php');
    exit;
}

require_once 'class21.php';

$classModel = new Classes();
$classes = $classModel->getClasses();

$error = '';
$success = '';

// 处理删除请求
$action = $_GET['action'] ?? '';
$id = $_GET['id'] ?? '';

if ($action == 'delete' && $id) {
    try {
        $classModel->deleteClass($id);
        header('Location: classes21.php?success=班级删除成功');
        exit;
    } catch (Exception $e) {
        $error = $e->getMessage();
    }
}

// 获取成功消息
if (isset($_GET['success'])) {
    $success = $_GET['success'];
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>教学服务系统 - 班级管理</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="header">
        <div class="container">
            <h1>教学服务系统</h1>
        </div>
    </div>

    <div class="main-nav">
        <div class="container">
            <ul>
                <li><a href="index.php">主页</a></li>
                <li><a href="departments21.php">系部管理</a></li>
                <li><a href="classes21.php" class="active">班级管理</a></li>
                <li><a href="students21.php">学生管理</a></li>
                <li><a href="teachers21.php">教师管理</a></li>
                <li><a href="courses21.php">课程管理</a></li>
                <li><a href="scores21.php">成绩管理</a></li>
                <li><a href="teachings21.php">授课管理</a></li>
                <li class="logout"><a href="logout21.php">退出登录</a></li>
            </ul>
        </div>
    </div>

    <div class="container">
        <div class="card">
            <div class="card-header">
                <h2 class="card-title">班级管理</h2>
            </div>
            
            <?php if ($error): ?>
            <div class="message message-error"><?php echo $error; ?></div>
            <?php endif; ?>
            
            <?php if ($success): ?>
            <div class="message message-success"><?php echo $success; ?></div>
            <?php endif; ?>
            
            <div class="card-body">
                <a href="add_class21.php" class="btn btn-primary">添加班级</a>
                
                <table class="table">
                    <thead>
                        <tr>
                            <th>班级ID</th>
                            <th>班级名称</th>
                            <th>所属系部</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($classes as $class): ?>
                        <tr>
                            <td><?php echo $class['class_id']; ?></td>
                            <td><?php echo $class['class_name']; ?></td>
                            <td><?php echo $class['department_name']; ?></td>
                            <td>
                                <a href="edit_class21.php?id=<?php echo $class['class_id']; ?>" class="btn btn-secondary">编辑</a>
                                <a href="classes21.php?action=delete&id=<?php echo $class['class_id']; ?>" 
                                   class="btn btn-danger" 
                                   onclick="return confirm('确定要删除该班级吗？删除后无法恢复！')">删除</a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>
</html>